package com.edou.member.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.base.page.PageResult;
import com.edou.member.dao.IMemberCollectionDao;
import com.edou.member.dao.IMemberUserDao;
import com.edou.member.entity.MemberCollection;
import com.edou.member.entity.MemberUser;
import com.edou.member.service.IMemberCollectionService;

import com.edou.member.dto.MemberCollectionCondition;

/**
 * 会员收藏
 * @creator     
 * @create-time 2016/4/18 17:15:31
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Service("memberCollectionServiceImpl")
@Transactional(value = "transactionManager")
public class MemberCollectionServiceImpl implements IMemberCollectionService{

	@Resource(name = "memberCollectionDaoImpl")
	private IMemberCollectionDao memberCollectionDao;
	@Resource(name = "memberUserDaoImpl")
	private IMemberUserDao memberUserDao;

	/**
	 * 查询所有会员收藏
	 */
	public List<MemberCollection> listMemberCollectionAll(){
		return memberCollectionDao.listMemberCollectionAll();
	}

	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listMemberCollectionByPage(PageResult<MemberCollection> pageResult,MemberCollectionCondition condition){
		int rows = memberCollectionDao.listMemberCollectionByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<MemberCollection> list = memberCollectionDao.listMemberCollectionByPage(rowBounds,condition);
		pageResult.setResult(list);
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 会员收藏
	 */
	public MemberCollection getMemberCollectionById(Long id){
		return memberCollectionDao.getMemberCollectionById(id);
	}
	
	/**
	 * 根据实体ID、类型与会员ID查询是否已经收藏了该实体
	 * @param objectId
	 * @param objectType
	 * @param memberId
	 * @return
	 */
	public MemberCollection getMemberCollectionByObjectIdTypeMemberId(String objectId, Integer objectType, String memberId){
		return memberCollectionDao.getMemberCollectionByObjectIdTypeMemberId(objectId, objectType, memberId);
	}

	/**
	 * 新增
	 * @param item 会员收藏
	 */
	public void add(MemberCollection item){
		memberCollectionDao.add(item);
	}

	/**
	 * 修改
	 * @param item 会员收藏
	 */
	public void update(MemberCollection item){
		memberCollectionDao.update(item);
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(String[] ids){
		memberCollectionDao.delByIds(ids);
	}

	@Override
	public int getMemberCollectionByMemberIdCount(String memberId) {
		return memberCollectionDao.getMemberCollectionByMemberIdCount(memberId);
	}

	@Override
	public void listMemberCollectionByObjectIdAndObjectTypePage(PageResult<MemberCollection> pageResult,
			MemberCollectionCondition condition) {
		// TODO Auto-generated method stub
		int rows = memberCollectionDao.listMemberCollectionByObjectIdAndObjectTypePageCount(condition);
		pageResult.setRows(rows);
		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<MemberCollection> list = memberCollectionDao.listMemberCollectionByObjectIdAndObjectTypePage(rowBounds,condition);
		for (MemberCollection memberCollection : list) {
			MemberUser member = memberUserDao.getMemberUserById(memberCollection.getMemberUserId());
			memberCollection.getMemberUser().setNickName(member.getNickName());
			memberCollection.getMemberUser().setFacePath(member.getFacePath());
		}
		pageResult.setResult(list);
	}

	/**
	 * 根据条件查询数据
	 */
	@Override
	public List<MemberCollection> listMemberCollectionCondition(MemberCollectionCondition condition) {
		return memberCollectionDao.listMemberCollectionCondition(condition);
	}

	/**
	 * 根据主键ID查询
	 */
	@Override
	public void delById(Long id) {
		memberCollectionDao.delById(id);
	}

	/**
	 * 取消收藏
	 * @param objectId
	 */
	public void cancelCollection(String objectId,Integer objectType) {
		memberCollectionDao.cancelCollection(objectId,objectType);
	}
	
	/**
	 * 根据收藏对象ID、收藏类型、收藏会员ID进行取消收藏
	 */
	@Override
	public void delCollection(String objectId, Integer objectType, String memberId) {
		memberCollectionDao.delCollection(objectId, objectType, memberId);
	}

	/**
	 * 查询当前文章收藏的总数
	 */
	@Override
	public int getMemberCollectionByObjectIdCount(String objectId) {
		return memberCollectionDao.getMemberCollectionByObjectIdCount(objectId);
	}
	


}

